package com.cdtu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.entity.Certify;
import com.cdtu.entity.RUserCertify;
import com.cdtu.mapper.CertifyTypeMapper;
import com.cdtu.mapper.RUserCertifyMapper;
import com.cdtu.service.CertifyService;
import com.cdtu.mapper.CertifyMapper;
import com.cdtu.vo.CertifyDTO;
import com.cdtu.vo.SpecialityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Sakura
* @description 针对表【certify】的数据库操作Service实现
* @createDate 2023-01-13 19:21:23
*/
@Service
public class CertifyServiceImpl extends ServiceImpl<CertifyMapper, Certify>
    implements CertifyService{

    @Autowired
    private CertifyMapper certifyMapper;

    @Autowired
    private CertifyTypeMapper certifyTypeMapper;

    @Autowired
    private RUserCertifyMapper rUserCertifyMapper;

    @Override
    public List<String> findByIds(List<Long> ids) {

        //如果ids为0，则返回空的集合
        if (ids.size()==0){
            List<String> result=new ArrayList<>();
            return result;
        }

        List<Certify> certifies = certifyMapper.selectBatchIds(ids);
        List<String> certifyName=new ArrayList<>();

        for (Certify certify : certifies) {
            certifyName.add(certify.getName());
        }
        return certifyName;
    }

    @Override
    public List<Long> findByType(long typeId) {

        return certifyMapper.findByCertify_id(String.valueOf(typeId));
    }

    @Override
    public List<CertifyDTO> findByIdsToUpdate(List<Long> ids, long uid) {
        //如果ids为0，则返回空的集合
        if (ids.size()==0){
            List<CertifyDTO> result=new ArrayList<>();
            return result;
        }

        List<Certify> certifies = certifyMapper.selectBatchIds(ids);

        //要返回的集合
        List<CertifyDTO> certifyList=new ArrayList<>();

        for (Certify certify : certifies) {
            Long cid = certify.getId();

            CertifyDTO dto=new CertifyDTO();
            dto.setName(certify.getName());
            dto.setId(cid);

            //根据证书查询证书类型
            Long typeId = certifyMapper.selectById(cid).getTypeId();
            String typeName = certifyTypeMapper.selectById(typeId).getName();
            dto.setType(typeName);

//            根据人员证书管理查询证书详细地址
//            String imageSrc = rUserCertifyMapper.findImageSrcByUserIdAndCertifyId(uid, cid);

//            RUserCertify rUserCertify = rUserCertifyMapper.findImageSrcAndIdByUserIdAndCertifyId(uid, cid);
            LambdaQueryWrapper<RUserCertify> queryWrapper=new LambdaQueryWrapper();
            queryWrapper.eq(RUserCertify::getUserId,uid);
            queryWrapper.eq(RUserCertify::getCertifyId,cid);
//            RUserCertify rUserCertify = rUserCertifyMapper.selectOne(queryWrapper);

            List<RUserCertify> rUserCertifyList = rUserCertifyMapper.selectList(queryWrapper);
            RUserCertify rUserCertify = rUserCertifyList.get(0);

            String imageSrc = rUserCertify.getImageSrc();
            dto.setRuc(rUserCertify.getId());

            dto.setImageUrl(imageSrc);
            dto.setDeadline(rUserCertify.getDeadline());
//            将dto对象添加到集合中
            certifyList.add(dto);
        }

        return certifyList;
    }
}




